<?php

/**
 * factory actions.
 *
 * @package    WacStorehouse
 * @subpackage factory
 * @author     JianBinBi <prince.bi@gmail.com>
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class factoryActions extends WacCommonActions {
    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetCottonFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryCotton);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetDyeFactoryIdNameHashInFormat(sfWebRequest $request) {
        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryDye);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as request format
    */
    public function executeGetCustomerFactoryIdNameHashInFormat(sfWebRequest $request) {
        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryCustomer);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetWeaveFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryWeave);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetCleanUpFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByType(WacFactoryType::$factoryCleanUp);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  get dye and weave factorys
   *  return id=>name hash as request format
    */
    public function executeGetDyeWeaveFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();

        $types = array(WacFactoryType::$factoryWeave, WacFactoryType::$factoryDye);
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
    *  get weave and cleanup factorys
    *  return id=>name hash as request format
    */
    public function executeGetWeaveCleanUpFactoryIdNameHashInFormat(sfWebRequest $request) {
        $resultSet = array();

        $types = array(WacFactoryType::$factoryWeave, WacFactoryType::$factoryCleanUp);
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
   *  apply different dataFormater according to request params "data_format"
   *  return id=>name hash as request format
    */
    public function executeGetFinalClothFactoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward($this->getModuleName(), "GetFinalFactoryIdNameHashInFormat");
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as request format
    */
    public function executeGetFinalClothQcFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();
        $types = array(WacFactoryType::$factoryFinalClothQC);
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }


    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as request format
    */
    public function executeGetFinalFactoryIdNameHashInFormat(sfWebRequest $request) {

        $resultSet = array();
        $types = array(WacFactoryType::$factoryWeave, WacFactoryType::$factoryCleanUp, WacFactoryType::$factoryFinalClothQC);
        if($request->hasParameter("types")){
            $types = $request->getParameter("types");
        }
        
        $items = Doctrine::getTable(WacTable::$factory)->getIdNameHashListByTypes($types);
        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);

        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockGoodsCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"));

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }


    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockGoodsIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $items = $objFactory->getStockGoodsIdNameHash($request->getParameter("q"));

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return
    */
    public function executeGetStockGoodsList(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $jqGridDataHelper = JqGridDataHelper::getInstance();
        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();
        $arrParam = array();

        if($request->hasParameter("limitGoodsCategorysCodes")) {
            $limitGoodsCategorysIds = Doctrine::getTable(WacTable::$goodsCategory)->getIdsByCodes($request->getParameter("limitGoodsCategorysCodes"));
            $resultSet = $objFactory->getStockGoodsList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false, $limitGoodsCategorysIds);
        }
        else {
            $resultSet = $objFactory->getStockGoodsList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false);
        }

        $pager = Doctrine::getTable(WacTable::$factoryGoodsItem)->getPager();

        $filterResultSet = $this->filterGoodsList($resultSet);
        $filterResultSet = $jqGridDataHelper->convert($filterResultSet, $pager, JsCommonData::getSuccessDatum());
        return OutputHelper::getInstance()->outputJsonOrTextFormat($filterResultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockFillingYarnGoodsList(sfWebRequest $request) {
        $request->setParameter("limitGoodsCategorysCodes", array(WacGoodsCategory::$predefinedClassCodes["classCottonCode"]));
        $this->forward($this->getModuleName(), "GetStockGoodsList");
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockWarpYarnGoodsList(sfWebRequest $request) {
        $request->setParameter("limitGoodsCategorysCodes", array(WacGoodsCategory::$predefinedClassCodes["classDyeCode"]));
        $this->forward($this->getModuleName(), "GetStockGoodsList");
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return
    */
    public function executeGetStockGoodsListByDispatchOrderType(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $jqGridDataHelper = JqGridDataHelper::getInstance();
        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();
        $arrParam = array();

        $limitGoodsCategorysIds = array();
        if($request->hasParameter("type")) {
            $limitGoodsCategorysIds = WacDispatchOrder::getLimitGoodsCategorys(WacBizOrderType::$type[$request->getParameter("type")]["code"]);
        }

        $resultSet = $objFactory->getStockGoodsList($arrParam, $request->getParameter(JqGridDataHelper::$KEY_PAGE), $request->getParameter(JqGridDataHelper::$KEY_ROWS), false, $limitGoodsCategorysIds);
        $pager = Doctrine::getTable(WacTable::$factoryGoodsItem)->getPager();

        $filterResultSet = $this->filterGoodsList($resultSet);
        $filterResultSet = $jqGridDataHelper->convert($filterResultSet, $pager, JsCommonData::getSuccessDatum());
        return OutputHelper::getInstance()->outputJsonOrTextFormat($filterResultSet, $this);
    }

    public function filterGoodsList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['goods_name'] = $listObj->getWacGoods()->getName();
                $tmpArr['goods_unit_code_name'] = $listObj->getWacGoodsNumberUnitCode()->getName();
                $tmpArr['goods_number_unit_code_name'] = $tmpArr['goods_unit_code_name'];
                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockWarpYarnGoodsIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classDyeCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr, array("valueFormat"=>"detail"));

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockWarpYarnCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classDyeCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockFillingYarnCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));
        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockFillingYarnGoodsIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classCottonCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr, array("valueFormat"=>"detail"));

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockWeaveGoodsIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $goodsCategory = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classWeaveCode"]);
        $limitCategorys = array(
                $goodsCategory->getId()
        );

        $limitStr="in";

//        $items = $objFactory->getStockGoodsIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr, array("valueFormat"=>"detail"));
        $items = $objFactory->getStockGoodsIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
     *  apply different dataFormater according to request params "data_format"
     *  return id=>name hash as select html format
    */
    public function executeGetStockWeaveCategoryIdNameHashInFormat(sfWebRequest $request) {
        $this->forward404Unless($request->hasParameter('factory_id'));

        $objFactory = Doctrine::getTable(WacTable::$factory)->findOneById($request->getParameter('factory_id'));

        $resultSet = array();

        $objGoodsCategory1 = Doctrine::getTable(WacTable::$goodsCategory)->findOneBy("Code", WacGoodsCategory::$predefinedClassCodes["classWeaveCode"]);
        $limitCategorys = array(
                $objGoodsCategory1->getId()
        );

        $limitStr="in";

        $items = $objFactory->getStockGoodsCategoryIdNameHash($request->getParameter("q"), $limitCategorys, $limitStr);

        $resultSet = OutputHelper::getInstance()->datumFormat($items, $request);
        return OutputHelper::getInstance()->outputJsonOrTextFormat($resultSet, $this);
    }

    /*
    * override filter list
    */
    public function filterList($listObjs) {
        $filterArr = array();
        if(count($listObjs) > 0) {
            foreach($listObjs as $listObj) {
                $tmpArr = $listObj->toArray();
                $tmpArr['type'] = $listObj->getWacFactoryType()->getName();
                $tmpArr['area_size_unit_code']    = $listObj->getWacAreaSizeUnit()->getName();
                $tmpArr['capacity_unit_code']     = $listObj->getWacCapacityUnit()->getName();
                $tmpArr['goods_amount_unit_code'] = $listObj->getWacGoodsAmountUnit()->getName();

                $filterArr[] = $tmpArr;
            }
        }

        return $filterArr;
    }
}